--require "MainScene"
JiaSuDialog={}
JiaSuDialogFunc={}
JiaSuDialogFunc.__index = JiaSuDialogFunc
setmetatable(JiaSuDialog, JiaSuDialogFunc)

JiaSuDialogFunc.create = function(self, bid, target, action)
  local l = {}
  setmetatable(l, JiaSuDialogFunc)
  l.target = target
  l.action = action
  l.bid = bid 
  l.jiasuType = "Building" 
  l.leftTime = UpgradeManager.buildingScenes[bid].upgradeCooldown
  l.node = CCBReader:load("JiaSuDialog.ccbi")
  setObjectExtTable(l.node:getChildByTag(1111), l)
  
  setObjectExtTable(l.node:getChildByTag(1020), l)
  setObjectExtTable(l.node:getChildByTag(1021), l)
  
  local min = math.ceil(l.leftTime/60) 
  local tmp = string.format("当前剩余%s，是否需要加速完成？需要消耗%d个时间卷轴或%d个军饷。", formatSecond(l.leftTime), min, min)
  TypeCast(l.node:getChildByTag(1011), "CCLabelTTF"):setString(autoEnter(tmp, 48)) 
 
  --加速卷轴id=11 
  TypeCast(l.node:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%d/%d", min, ItemManager:getItemCount(11))) 
  TypeCast(l.node:getChildByTag(1013), "CCLabelTTF"):setString(string.format("%d/%d", min, UserData.userinfo.host.coin)) 

  setTouchEnabledAll(target.node, false)
  
  target.node:getParent():addChild(l.node)
  return l 
end

JiaSuDialogFunc.createExt = function(self, jiasuType, leftTime, replacedNode, handler)
  local l = {}
  setmetatable(l, JiaSuDialogFunc)
  --l.target = target
  --l.action = action
  l.handler = handler
  l.jiasuType = jiasuType 
  l.replacedNode = replacedNode
  l.node = CCBReader:load("JiaSuDialog.ccbi")
  setObjectExtTable(l.node:getChildByTag(1111), l)
  
  setObjectExtTable(l.node:getChildByTag(1020), l)
  setObjectExtTable(l.node:getChildByTag(1021), l)
  
  local min = math.ceil(leftTime/60) 
  local tmp = string.format("当前剩余%s，是否需要加速完成？需要消耗%d个时间卷轴或%d个军饷。", formatSecond(leftTime), min, min)
  TypeCast(l.node:getChildByTag(1011), "CCLabelTTF"):setString(autoEnter(tmp, 48)) 
 
  --加速卷轴id=11 
  TypeCast(l.node:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%d/%d", min, ItemManager:getItemCount(11))) 
  TypeCast(l.node:getChildByTag(1013), "CCLabelTTF"):setString(string.format("%d/%d", min, UserData.userinfo.host.coin)) 

  l.replacedNode:setVisible(false)
  --setTouchEnabledAll(l.replacedNode, false)
  l.replacedNode:getParent():addChild(l.node)
  return l
end

JiaSuDialogFunc.onJuanZhouButton = function(self, node, event)
  --setTouchEnabledAll(self.node.getParent(), true)
  node:getParent():getParent():removeChild(node:getParent(), true)
  LoadScene:showLoadingScene()
  print("JiaSuDialogFunc.onJuanZhouButton", self.jiasuType)
  if self.jiasuType=="Building" then
    NetworkRequest.accelerate("Upgrading", self.bid, "onJiaSuComplete", self)
  elseif self.jiasuType=="JingJiChang" then
    NetworkRequest.clearJingJiChangCD("onJiaSuComplete", self)
  else
    NetworkRequest.test("onJiaSuComplete", self)
  end
end

JiaSuDialogFunc.onClose = function(self, node, event)
  self.replacedNode:setVisible(true)
  --setTouchEnabledAll(self.replacedNode, true)
  node:getParent():getParent():removeChild(node:getParent(), true)
  
  --setTouchEnabledAll(self.target.node, true)
end

JiaSuDialogFunc.onJiaSuComplete = function(self, data)
  print("JiaSuDialogFunc.onJiaSuComplete", data)
  self.replacedNode:setVisible(true)
  --setTouchEnabledAll(self.replacedNode, true)
  --[[
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage("success", ccc3(0,255,0))
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  ]]
  LoadScene:removeLoadingScene()
  
  --if self.bid~=nil then
  if self.jiasuType=="Building" then
    UpgradeManager.buildingScenes[self.bid].upgradeCooldown = 0
  elseif self.jiasuType=="JingJiChang" then
    JingJiChangScene.fightCD = 0
  end
  if self.handler~=nil then
    local metaFunc = getmetatable(self.handler.target)
    metaFunc[self.handler.action](self.handler.target)
  end
end

